/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.dialogs;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import org.apache.commons.validator.GenericValidator;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.mags.remas.view.commons.ImageProvider;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class MessageDialog extends org.eclipse.jface.dialogs.MessageDialog {
	private Text technicalDetails;
	final private Throwable throwable;	
	public Throwable getThrowable() {
		return throwable;
	}
	public MessageDialog(Shell parentShell, String dialogTitle,
			Image dialogTitleImage, String dialogMessage, int dialogImageType,
			String[] dialogButtonLabels, int defaultIndex, Throwable throwable) {
		super(parentShell, dialogTitle, dialogTitleImage, dialogMessage,
				dialogImageType, dialogButtonLabels, defaultIndex);
		super.setDefaultImage(ImageProvider.getCommonImage("error.png"));
		this.throwable = throwable;
	}
    @Override
    protected Control createCustomArea(final Composite parent) {
    	if(throwable!=null) {
	    	Composite buttons = new Composite(parent, SWT.NONE);
	    	buttons.setLayout(new GridLayout(1, true));
			GridData cellLayoutData = new GridData();
			cellLayoutData.horizontalAlignment = SWT.RIGHT;
			cellLayoutData.grabExcessHorizontalSpace = true;
	    	buttons.setLayoutData(cellLayoutData);    	
	    	Button button = new Button(buttons, SWT.NONE);
	    	button.setText("Technical Details");
	    	button.addSelectionListener(new SelectionListener() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					if(technicalDetails!=null) {
						technicalDetails.dispose();
						technicalDetails = null;
						getShell().pack();
					} else {
						technicalDetails = new Text(parent, SWT.BORDER|SWT.MULTI|SWT.V_SCROLL|SWT.H_SCROLL|SWT.READ_ONLY);
				        GridData gridData = new GridData(GridData.FILL_BOTH);
				        gridData.grabExcessHorizontalSpace = true;
				        gridData.grabExcessVerticalSpace = true;
				        gridData.horizontalSpan = 2;
				        gridData.heightHint = 150;
				        technicalDetails.setLayoutData(gridData);
				        getShell().pack();
						try {
							ByteArrayOutputStream out = new ByteArrayOutputStream();
							PrintWriter p = new PrintWriter(out);
							throwable.printStackTrace(p);
							p.flush();
							p.close();
							if(!GenericValidator.isBlankOrNull(out.toString()))
								technicalDetails.setText(out.toString());
							out.close();
						} catch (IOException e1) {/*IGNORE EXCEPTION*/}
					}
				}
				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
				}
	    	});
	        return parent;
    	} else
    		return null;
    }	
    public static void openError(Shell parent, String title, String message, Throwable throwable) {
		MessageDialog dialog = new MessageDialog(parent, title, null, message,
				ERROR, new String[] { IDialogConstants.OK_LABEL}, 0, throwable);
		dialog.setShellStyle(dialog.getShellStyle() | SWT.SHEET | SWT.RESIZE);
		dialog.open();
    }    
    public static boolean openErrorAndQuestion(Shell parent, String title, String message, Throwable throwable) {
		MessageDialog dialog = new MessageDialog(parent, title, null, message,
				ERROR, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 0, throwable);
		dialog.setShellStyle(dialog.getShellStyle() | SWT.SHEET | SWT.RESIZE);
		return dialog.open() == 0;
    }
}
